﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Maticsoft.BLL;
using Maticsoft.Model;
using ExtendDLL;
using System.Text;
namespace lingxianWebservice
{
    /// <summary>
    /// 查询数据 ，当前只支持按压力进行查询数据，后期可以进行扩展 sndddx 20160421
    /// getSearch 的摘要说明
    /// </summary>
    public class getSearch : IHttpHandler
    {
        private static readonly Maticsoft.BLL.BaseSql exsql = new Maticsoft.BLL.BaseSql();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string returnString = string.Empty;
            string mType = context.Request["pType"].ToString().Trim();
            string mSQL = " WHERE 1=1 AND ((ZDID >0 AND zdid<500) OR ZDID>600) ";
            switch (mType)
            {
                case "1"://查询压力范围数据
                    string mMinValue = context.Request["MinValue"].ToString().Trim();
                    string mMaxValue = context.Request["MaxValue"].ToString().Trim();
                    float mMin;
                    float mMax;
                    if (null != mMinValue && null != mMaxValue && mMinValue.Length > 0 && mMaxValue.Length > 0)
                    {
                        float.TryParse(mMinValue, out mMin);
                        float.TryParse(mMaxValue, out mMax);
                        mSQL += " AND CAST(lx_326 AS FLOAT)>=" + mMin.ToString() + " AND CAST(lx_326 AS FLOAT)<=" + mMax.ToString();
                    }
                    else if ((null != mMinValue && mMinValue.Length > 0) && (null == mMaxValue || mMaxValue.Length == 0))
                    {
                        float.TryParse(mMinValue, out mMin);
                        mSQL += " AND CAST(lx_326 AS FLOAT)>=" + mMin.ToString();
                    }
                    else if ((null == mMinValue || mMinValue.Length == 0) && (null != mMaxValue && mMaxValue.Length > 0))
                    {
                        float.TryParse(mMaxValue, out mMax);
                        mSQL += " AND CAST(lx_326 AS FLOAT)<=" + mMax.ToString();
                    }
                    else
                    {
                        mSQL = " AND 1=1 ";
                    }

                    break;
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT * FROM ( SELECT SJ,[324] as lx_324,[325] as lx_325,[326] as lx_326,[327] as lx_327,[328] as lx_328, ");
            sb.Append(" a.SSLL AS SSLL_one,SBH,b.CardNum,b.FBCS,b.FWBH,b.FZT,b.KZZT,b.ModelID, ");
            sb.Append(" b.QYID,b.SBZT,b.SB_Authorization,b.SCJG,b.SCJG_history,b.SCSJ_m, ");
            sb.Append(" b.SCSJ_s,b.SSLL AS SSLL_ZDXX,b.SX,b.WZXX,b.xszt,b.YWSD,b.ZBCS, ");
            sb.Append(" b.ZBX,b.ZBY,b.ZDBH,b.ZDID,b.ZDLX,b.ZDMS ");
            sb.Append(" FROM ZB_Data_all_one AS a INNER JOIN ZB_ZDXX AS b ON a.SBH=b.ZDBH  ) as t ");
            sb.Append(mSQL);
            var points = exsql.getList(sb.ToString());


            if (null == points || points.Tables[0].Rows.Count == 0)
            {
                context.Response.Write("error:当前没有数据");
            }
            else
            {
                string callbackFunName = context.Request["callbackparam"];
                string str = callbackFunName + "(" + points.Tables[0].toJSON() + ")";
                context.Response.Write(str);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}